/*
 * @Author: xiaoyan
 * @Date: 2021-11-22 15:23:18
 * @LastEditors: xiaoyan
 * @LastEditTime: 2021-11-26 15:45:46
 * @Description: 
 * @FilePath: \user_auth_front\src\store\modules\app.ts
 */
import {modalConf} from "@/constant"

const SET_COLLAPSED = "set_collapsed";
const SET_MODALCONF = "set_modalConf";
const SET_MINMODALCONF = "set_minModalConf";

interface MyState {
    collapsed:boolean,
    modalConf:Object,
    minModalConf:object,
}

const defaultState:MyState = {
    collapsed:true,//sideBar是否展开，默认true
    modalConf,//modal配置项
    minModalConf:modalConf,//副modal配置项
}

type TSetCollapsed = {
    type: typeof SET_COLLAPSED;
    payload:boolean;
}
type TSetModalConf = {
    type: typeof SET_MODALCONF;
    payload:Object;
}
type TSetMinModalConf = {
    type: typeof SET_MINMODALCONF;
    payload:Object;
}

export type Actions = TSetCollapsed | TSetModalConf | TSetMinModalConf;

export default (state: MyState = defaultState, action: Actions) =>{
    switch (action.type){
        case SET_COLLAPSED:
            return Object.assign({},state,{
                collapsed:action.payload
            });
        case SET_MODALCONF:
            const conf = Object.assign({},state.modalConf,{
                ...action.payload
            });
            return Object.assign({},state,{modalConf:conf});
        case SET_MINMODALCONF:
            const mConf = Object.assign({},state.minModalConf,{
                ...action.payload
            });
            return Object.assign({},state,{minModalConf:mConf});
        default:
            return state;
    }
}

export const setCollapsed = (value:boolean)=>(dispatch:any) =>(//sideBar是否展开
    dispatch({
        type:SET_COLLAPSED,
        payload: value
    })
);

export const openModal = (value: Object)=>({//打开modal
        type:SET_MODALCONF,
        payload: Object.assign(value,{isModalVisible:true})
});

export const closeModal = ()=>({//关闭modal
    type:SET_MODALCONF,
    payload: {
        ...modalConf
    }
});

export const openMinModal = (value: Object)=>({//打开副modal
    type:SET_MINMODALCONF,
    payload: Object.assign(value,{isModalVisible:true})
});
export const closeMinModal = ()=>({//关闭副modal
    type:SET_MINMODALCONF,
    payload: {
        ...modalConf
    }
});